package com.ydgk.servlet;

import com.ydgk.dao.SupplierDao;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "SupplierDeleteServlet", urlPatterns = "/SupplierDeleteServlet.action")
public class SupplierDeleteServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sid = request.getParameter("sid");
        String pageNumber = request.getParameter("pageNumber");
        SupplierDao supplierdao = new SupplierDao();
        int i = supplierdao.deleteById(Integer.parseInt(sid));
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(i>0){
            RequestDispatcher rd = request.getRequestDispatcher("SupplierPagingServlet.action?pageNumber="+pageNumber);
            rd.forward(request,response);
        }else {
            out.print("<link rel='stylesheet' type='text/css' href='css/suppliers.css'>");
            out.write("<body></body><script>alert('无法删除！库中还有该供应商提供的书籍哦~');window.history.back();</script>");
        }
    }


}
